import ttkbootstrap as ttk
from os.path import join, dirname
from sys import executable
from subprocess import Popen, DETACHED_PROCESS
from math import sqrt, floor, ceil

#提高鲁棒性，算是一种相对路径
DIR = dirname(__file__)
#APPs列表就是功能模块
APPs = ["FBSerial","FBImg"]

if __name__ == "__main__":
    style = ttk.Style("cosmo")
    root = style.master
    root.title("launcher")
    root.resizable(False, False)
    C = ceil(len(APPs) / floor(sqrt(len(APPs))))
    for i, app in enumerate(APPs):
        #根据APPs列表长度来调整布局
        #当按钮按下的时候command调用Popen调用子进程，也即同名文件夹中的main.py,然后再从main.py中通过import其他模块来组织起来
        ttk.Button(
            root,
            text=app,
            command=lambda app=app: Popen([executable, join(DIR, app, "main.py")], creationflags=DETACHED_PROCESS),
        ).grid(row=i // C, column=i % C, padx=5, pady=5, sticky="nsew")
    root.mainloop()
